﻿<%@ WebHandler Language="C#" Class="ShowListBill" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class ShowListBill : AjaxHandlerBase {
    
 //*******************************************
    //功能概要       ：基本权限初始化设定
    //参数説明       ： 
    //返回值説明     ：-
    //功能詳細       ：-
    //作成者         ：chen.yugong 
    //作成日         ：2010-06-29
    //修改者         ：
    //修改日         ：2010-07-13
    //*******************************************
    public ShowListBill()
    {
        //开单作废
        htTypePermit.Add("cancelbill", Constants.Permit_CancelBillInfo);
        //收付款登记
        htTypePermit.Add("regpayment", Constants.Permit_RegPayment);
        //复核
        htTypePermit.Add("check", Constants.Permit_CheckBillInfo);
        //反复核
        htTypePermit.Add("uncheck", Constants.Permit_UnCheckBillInfo);
        //解除封帐
        htTypePermit.Add("openaccount", Constants.Permit_OpenAccout);
         //打印发票
        htTypePermit.Add("printinvoice", Constants.Permit_PrintInvoice);
        //导出
        htTypePermit.Add("export", Constants.Permit_Bill_Export);
        htTypePermit.Add("exportall", Constants.Permit_Bill_Export);
    }

     //*******************************************
    //功能概要       ：请求处理
    //参数説明       ：context 请求上下文
    //返回值説明     ：-
    //功能詳細       ：根据请求名 分发请求
    //作成者         ：he.jifeng
    //作成日         ：2010-06-29
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string strRet = "";
        try
        {
            string strOp = context.Request["op"];
            switch (strOp.ToLower())
            {
                case "getlist":
                    strRet = getBillList(context);
                    break;
                //开单作废
                case "cancelbill":
                    strRet = cancelBillInfo(context);
                    break;
                //收付款登记
                case "regpayment":
                    strRet = regPayment(context);
                    break;
                //复核
                case "check":
                    strRet = checkBillInfo(context);
                    break;
                //反复核
                case "uncheck":
                    strRet = unCheckBillInfo(context);
                    break;
                //作废收付款
                case "cancelpayment":
                    strRet = cancelPayment(context);
                    break;
                //登记发票
                case "reginvoice":
                    strRet = regInvoice(context);
                    break;
                //发票领取记录
                case "reginvoicerev":
                    strRet = regInvoiceRev(context);
                    break;
                //财务封帐
                case "closeaccout":
                    strRet = closeAccount(context);
                    break;
                //解封
                case "openaccount":
                    strRet = openAccount(context);
                    break;
                //打印发票
                case "printinvoice":
                    strRet = printInvoice(context);
                    break;
                //得到发票打印内容
                case "getprintmodel":
                    strRet = getPrintModel(context);
                    break;
                //获得所有数据不分页（导出用）
                case "exportall":
                    GetExportData(context);
                    break;
                default:
                    break;
            }

            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            context.Response.Write(strRet);
        }
    }

    private void GetExportData(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        string strHTML = string.Empty;
        Dictionary<string, string> dicParams = new Dictionary<string, string>();

        //开单明细业务类
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }
        strParam = context.Request.Params["Keyword"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Keyword", strParam);
        }
        strParam = context.Request.Params["Status"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Status", strParam);
        }
        strParam = context.Request.Params["datetype"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("datetype", strParam);
        }
        strParam = context.Request.Params["inorout"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("inorout", strParam);
        }
        strParam = context.Request.Params["IsFinish"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("IsFinish", strParam);
        }
        strParam = context.Request.Params["IsRecheck"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("IsRecheck", strParam);
        }
        strParam = context.Request.Params["FundName"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("FundName", strParam);
        }
        strParam = context.Request.Params["BillPlaceId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("BillPlace", strParam);
        }
        strParam = context.Request.Params["PayPlaceId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PayPlace", strParam);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }

        //查看所有
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllBillInfo))
        {
        }
        //查看本城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityBillInfo))
        {
            if (!dicParams.ContainsKey("CityId"))
            {
                dicParams.Add("CityId", CurrentUserInfo.CityId.ToString());
            }
        }
        //查看本公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyBillInfo))
        {
            if (!dicParams.ContainsKey("CompanyId"))
            {
                dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
        }
        //查看部门
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartBillInfo))
        {
            dicParams.Add("CurDepartId", CurrentUserInfo.UserId.ToString());
        }
        else
        {
            dicParams.Add("CurUserId", CurrentUserInfo.UserId.ToString());
        }

        DataSet dsBillInfo = ContractFinanceBillInfo.GetListByExport(dicParams);

        strHTML = GetHtml(dsBillInfo.Tables[0]);

        ExportByServce(context, strHTML);
    }
    
    
    //后台拼HTML（打印用）
    private string GetHtml(DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table class=\"tabsorternew table_archive\"><tr><th style=\"width:30px;\">封</th>");
        sb.Append("<th style=\"width:30px;\">核</th>");
        sb.Append("<th style=\"width:80px;\">商圈</th>");
        sb.Append("<th style=\"width:80px;\">门店</th>");
        sb.Append("<th style=\"width:40px;\">状态</th>");
        sb.Append("<th style=\"width:130px;\">成交编号</th>");
        sb.Append("<th style=\"width:130px;\">合同所属</th>");
        sb.Append("<th style=\"width:270px;\">房屋地址</th>");
        sb.Append("<th style=\"width:100px;\">客户姓名</th>");
        sb.Append("<th style=\"width:100px;\">款项名称</th>");
        sb.Append("<th style=\"width:120px;\">开单号</th>");
        sb.Append("<th style=\"width:70px;\">开单日期</th>");
        sb.Append("<th style=\"width:110px;\">开单金额</th>");
        sb.Append("<th style=\"width:100px;\">收款</th>");
        sb.Append("<th style=\"width:100px;\">付款</th>");
        sb.Append("<th style=\"width:70px;\">收付款日期</th>");
        sb.Append("<th style=\"width:60px;\">收款人</th>");
        sb.Append("<th style=\"width:60px;\">发票单号</th>");
        sb.Append("<th style=\"width:80px;\">发票日期</th>");
        sb.Append("<th style=\"width:60px;\">成交类型</th>");
        sb.Append("<th style=\"width:60px;\">归属日期</th>");
        sb.Append("<th style=\"width:200px;\">发票领取记录</th>");
        sb.Append("<th style=\"width:120px;\">开单地点</th>");
        sb.Append("<th style=\"width:120px;\">收付款地点</th></tr>");

        double billmoney = 0.0;     //开单金额总计
        double getmoney = 0.0;      //收款  
        double paymoney = 0.0;      //付款

        foreach (DataRow dr in dt.Rows)
        {
            sb.Append("<tr>");
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"IsFinish",false)==true?"是":"否");
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"IsRecheck",false) == true ? "是" : "否");
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"AreaName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"BillDepartName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"Status",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractCode",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractCompany",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"HouseAddress",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"CustomerName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"FundName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"BillCode",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"BillDate",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"TBillMoney",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"RecMoney",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"Payment",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PayDate",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PayOprEmpName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"InvoiceCode",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "InvoiceDate", ""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"BusinessType",""));
            sb.AppendFormat("<td>{0}.1</td>", DatasetHelper.GetValue(dr,"AchievementDate",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"InvoiceMemo",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"BillPlace",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PayPlace",""));
            sb.Append("</tr>");

            billmoney += string.IsNullOrEmpty(dr["TBillMoney"].ToString()) ? 0 : double.Parse(dr["TBillMoney"].ToString());
            getmoney += string.IsNullOrEmpty(dr["RecMoney"].ToString()) ? 0 : double.Parse(dr["RecMoney"].ToString());
            paymoney += string.IsNullOrEmpty(dr["Payment"].ToString()) ? 0 : double.Parse(dr["Payment"].ToString()); 
        }
        sb.AppendFormat("<tr><td></td><td></td><td>总合计</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>{0}</td><td>{1}</td><td>{2}</td>",billmoney,getmoney,paymoney);
        sb.Append("<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></table>");
        
        return sb.ToString();
    }
    
    

    //打印发票
    //tianno
    //2010-11-18
    private string getPrintModel(HttpContext context)
    {
        string billId = (string.IsNullOrEmpty(context.Request["billId"])) ? Guid.Empty.ToString() : context.Request["billId"];

        if (string.IsNullOrEmpty(billId)) return retExecResult(Constants.ExecResult.Error, "开单标识丢失", "");

        //获取开单信息
        Chenxin.Lib.Business.Entity.ContractFinanceBillInfo BillInfo = ContractFinanceBillInfo.GetModel(new Guid(billId));
        if (BillInfo == null) return retExecResult(Constants.ExecResult.Error, "获取发票信息失败，请联系管理员！", "");
        
        //获取合同信息
        Chenxin.Lib.Business.Entity.t_Contract_BaseInfo baseInfo = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetModelByCode(BillInfo.ContractCode);
        if (baseInfo == null) return retExecResult(Constants.ExecResult.Error, "获取发票信息失败，请联系管理员！", "");
        
        //获取开单明细
        List<Chenxin.Lib.Business.Entity.ContractFinancePayedItems> lsPayItems = Chenxin.Lib.Business.DAL.ContractFinancePayedItems.GetListArray("billid = '" +billId +"'");

        StringBuilder sb = new StringBuilder();
        sb.Append("<DIV style=\"BORDER-BOTTOM: #999 0px solid; BORDER-LEFT: #999 0px solid; WIDTH: 794px; FONT-FAMILY: Arial, Helvetica, sans-serif; HEIGHT: 529px; BORDER-TOP: #999 0px solid; BORDER-RIGHT: #999 0px solid;\">");
        sb.Append("<DIV style=\"PADDING-LEFT: 64px; PADDING-RIGHT: 20px; PADDING-TOP: 40px;\">");
        sb.Append("<DIV style=\"TEXT-ALIGN: center; HEIGHT: 35px;\"></DIV>");
        sb.Append("<DIV style=\"HEIGHT: 53px; PADDING-TOP: 10px;\"></DIV>");
        //客户姓名
        sb.AppendFormat("<DIV style=\"PADDING-LEFT: 45px; HEIGHT: 41px\"><DIV style=\"MARGIN-TOP: 20px; PADDING-LEFT: 60px; FLOAT: left; HEIGHT: 20px; FONT-SIZE: 14px;\">{0}</div>",BillInfo.CustomerName);
        sb.Append("<DIV style=\"TEXT-ALIGN: right; MARGIN-TOP: 20px; WIDTH: 150px; FLOAT: right;\">");
        //打印时间
        DateTime dt = DateTime.Now;
        sb.AppendFormat("<SPAN style=\"TEXT-ALIGN: center; WIDTH: 38px; DISPLAY: block; FLOAT: left; HEIGHT: 18px; MARGIN-LEFT: 20px; FONT-SIZE: 14px;\">{0}</SPAN>", dt.Year);
        sb.AppendFormat("<SPAN style=\"TEXT-ALIGN: center; WIDTH: 27px; DISPLAY: block; FLOAT: left; HEIGHT: 18px; MARGIN-LEFT: 10px; FONT-SIZE: 14px;\">{0}</span>",dt.Month);
        sb.AppendFormat("<SPAN style=\"TEXT-ALIGN: center; WIDTH: 27px; DISPLAY: block; FLOAT: left; HEIGHT: 18px; MARGIN-LEFT: 10px; FONT-SIZE: 14px;\">{0}</span></div>",dt.Day);
        sb.Append("</div>");
        sb.Append("<div>");
        sb.Append("<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\" width=\"680\"><TBODY>");
        sb.Append("<tr>");
        sb.Append("<TD style=\"height:58px; width:41px;\">&nbsp;</TD>");
        sb.Append("<TD style=\"width:239px;\">&nbsp;</TD>");
        sb.Append("<TD style=\"width:38px;\">&nbsp;</TD>");
        sb.Append("<TD style=\"width:55px;\">&nbsp;</TD>");
        sb.Append("<TD style=\"width:58px;\">&nbsp;</TD>");
        sb.Append("<TD style=\"width:134px;\">&nbsp;</TD>");
        sb.Append("<TD style=\"width:95px;\">&nbsp;</TD></TR>");
        sb.Append("<tr>");
        sb.Append("<TD height=\"166\">&nbsp;</TD>");
        sb.Append("<td valign=\"top\">");
        sb.Append("<DIV style=\"PADDING-BOTTOM: 10px; LINE-HEIGHT: 150%; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; WORD-WRAP: break-word; FONT-SIZE: 14px; OVERFLOW: hidden; PADDING-TOP: 10px;\">");
        //合同编号
        sb.AppendFormat("  {0}<br/>",BillInfo.ContractCode);
        //合同地址
        sb.AppendFormat("{0}<br/>",baseInfo.HouseAddress);
        sb.Append("<br/>服务费<br/>");
        
        //得到开单明细总数
        string memo = "";
        string amount = "";
        foreach (Chenxin.Lib.Business.Entity.ContractFinancePayedItems items in lsPayItems)
        {
            amount += "<DIV style=\"text-align: center; PADDING-BOTTOM: 10px; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; FONT-SIZE: 14px; VERTICAL-ALIGN: text-bottom; PADDING-TOP: 10px;\">" + items.PayMoney + "</div>";
            memo += "<DIV style=\"text-align: center; PADDING-BOTTOM: 10px; PADDING-LEFT: 10px; WIDTH: 65px; PADDING-RIGHT: 10px; WORD-WRAP: break-word; FONT-SIZE: 14px; OVERFLOW: hidden; PADDING-TOP: 10px;\">" + items.PayType + "</div>";
        }
        sb.Append("</div></td>");
        sb.Append("<td>&nbsp;</td>");
        sb.Append("<td>&nbsp;</td>");
        sb.Append("<td>&nbsp;</td>");
        sb.Append("<td valign=\"top\">");
        sb.Append(amount);
        sb.Append("</td>");
        sb.Append("<td valign=\"top\" rowspan=\"2\">");
        sb.Append(memo);
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.Append("<TD style=\"TEXT-ALIGN: left; PADDING-LEFT: 8px; FONT-WEIGHT: bold;\" height=45 colSpan=5><DIV style=\"WIDTH: 160px; FLOAT: left; HEIGHT: 30px;\"></DIV>");
        sb.Append("<DIV style=\"LINE-HEIGHT: 30px; WIDTH: 220px; FLOAT: left; HEIGHT: 30px; FONT-SIZE: 16px;\">");
        //合计大写
        string str = "";
        Chenxin.Lib.Utility.InitialHelper.TranslateNumberToChinese(BillInfo.BillMoney.ToString(), out str);
        sb.AppendFormat("{0}</div>",str);
        sb.Append("</td>");
        sb.Append("<td>");
        //合计金额
        sb.AppendFormat("<DIV style=\"TEXT-ALIGN: center; WIDTH: 100px; HEIGHT: 22px; MARGIN-LEFT: 10px; FONT-SIZE: 16px; FONT-WEIGHT: bold;\">{0}</div></td>",BillInfo.BillMoney);
        sb.Append("</tr></TBODY>");
        sb.Append("</table>");

        sb.Append("</div>");
        sb.Append("<DIV style=\"MARGIN-TOP: 10px; HEIGHT: 53px; FONT-SIZE: 12px;\">");
        //打印人
        sb.AppendFormat("<DIV style=\"LINE-HEIGHT: 38px; WIDTH: 125px; FLOAT: left; HEIGHT: 38px; MARGIN-LEFT: 300px; FONT-SIZE: 14px;\">{0}</div>",CurrentUserInfo.UserName);
        sb.Append("</div>");
        sb.Append("</div>");
        sb.Append("</div>");

        string text = CurrentUserInfo.UserName + "：打印发票。成交编号：" + BillInfo.ContractCode + "，单据编号：" + BillInfo.BillCode;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_PrintInvoice"].ToString());

        return string.Format("{{success:1,msg:'',data:{{model:'{0}'}}}}", sb.ToString()); ;
    }
    
    
    //打印发票
    //tianno
    //2010-11-18
    private string printInvoice(HttpContext context)
    {
        
        return "";
    }
    
    
    //*******************************************
    //功能概要       ：获得开单明细
    //参数説明       ：context 请求上下文
    //返回值説明     ：客户端显示数据
    //功能詳細       ：获得开单明细
    //作成者         ：he.jifeng
    //作成日         ：2010-06-29
    //*******************************************
    private string getBillList(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        StringBuilder strHtml = new StringBuilder();
        
        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        
        //开单明细业务类
        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["datetype"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("datetype", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }
        strParam = context.Request.Params["Keyword"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Keyword", strParam);
        }
        strParam = context.Request.Params["Status"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Status", strParam); 
        }
        strParam = context.Request.Params["FundName"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("FundName", strParam);
        }
        strParam = context.Request.Params["BillPlaceId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("BillPlace", strParam);
        }
        strParam = context.Request.Params["inorout"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("inorout", strParam);
        }
        strParam = context.Request.Params["IsFinish"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("IsFinish", strParam);
        }
        strParam = context.Request.Params["IsRecheck"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("IsRecheck", strParam);
        }
        strParam = context.Request.Params["PayPlaceId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PayPlace", strParam);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }
        
        //查看所有
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllBillInfo))
        {
        }
        //查看本城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityBillInfo))
        {
            if (!dicParams.ContainsKey("CityId"))
            {
                dicParams.Add("CityId", CurrentUserInfo.CityId.ToString());
            }
        }
        //查看本公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyBillInfo))
        {
            if (!dicParams.ContainsKey("CompanyId"))
            {
                dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
        }
        //查看部门
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartBillInfo))
        {
            dicParams.Add("CurDepartId", CurrentUserInfo.UserId.ToString());            
        }
        else
        {
            dicParams.Add("CurUserId", CurrentUserInfo.UserId.ToString());
        }

        DataSet dsBillInfo = ContractFinanceBillInfo.GetListByConditions(dicParams);

        strHtml.Append("{");
        strHtml.Append("success:'" + ((int)Constants.ExecResult.Succ).ToString() + "',");
        strHtml.Append("msg:'',");
        strHtml.AppendFormat("data:{{{0},", Chenxin.Lib.Utility.JsonHelp.Convert2Json(dsBillInfo.Tables[0]));
        strHtml.AppendFormat("count:'{0}'", dsBillInfo.Tables[1].Rows[0][0]);
        strHtml.Append("}");
        strHtml.Append("}");

        strRet = strHtml.ToString();

        return strRet;
    }

    //*******************************************
    //功能概要       ：开单作废
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：开单作废
    //作成者         ：he.jifeng
    //作成日         ：2010-09-03
    //*******************************************
    private string cancelBillInfo(HttpContext context)
    {
        string strRet = string.Empty;
        string ClientModel = context.Request.Params["model"];

        Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = (Chenxin.Lib.Business.Entity.ContractFinanceBillInfo)JsonHelp.Convert2Entity(ClientModel, typeof(Chenxin.Lib.Business.Entity.ContractFinanceBillInfo));

        Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entUpdBillInfo = ContractFinanceBillInfo.GetModel(entBillInfo.BillId);

        if (entUpdBillInfo.Status == -1 )
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Msg_CanceledBill, "");
        }
        else if (entUpdBillInfo.Status == 1)
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Msg_PayedBill, "");
        }
        else
        {
            entUpdBillInfo.UnusedDate = entBillInfo.UnusedDate;
            entUpdBillInfo.UnusedReason = entBillInfo.UnusedReason;
            entUpdBillInfo.UnusedPerson = CurrentUserInfo.UserName;
            entUpdBillInfo.UnusedPersonId = CurrentUserInfo.UserId;
            entUpdBillInfo.Status = -1;

            ContractFinanceBillInfo.Update(entUpdBillInfo);

            string text = CurrentUserInfo.UserName + "：开单作废。成交编号：" + entBillInfo.ContractCode + "，单据编号：" + entBillInfo.BillCode;
            AddLog(text, BusinessType.BusinessTypeComboList()["OprType_CancelBill"].ToString());

            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_CancelBill_Succ, "");
        }

        return strRet;
    }

    //*******************************************
    //功能概要       ：开单登记
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：开单登记
    //作成者         ：he.jifeng
    //作成日         ：2010-09-03
    //*******************************************
    public string regPayment(HttpContext context)
    {
        string strRet = string.Empty;
        string ClientModel = context.Request.Params["model"];
        string Payments = context.Request.Params["PayItems"];

        if (string.IsNullOrEmpty(ClientModel)) return retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");

        Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = (Chenxin.Lib.Business.Entity.ContractFinanceBillInfo)JsonHelp.Convert2Entity(ClientModel, typeof(Chenxin.Lib.Business.Entity.ContractFinanceBillInfo));
        List<Chenxin.Lib.Business.Entity.ContractFinancePayedItems> lsPayItems = (List<Chenxin.Lib.Business.Entity.ContractFinancePayedItems>)JsonHelp.Convert2Entity(Payments, typeof(List<Chenxin.Lib.Business.Entity.ContractFinancePayedItems>));

        Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entUpdBillInfo = ContractFinanceBillInfo.GetModel(entBillInfo.BillId);

        if (entBillInfo.PayMoney > entUpdBillInfo.BillMoney-entUpdBillInfo.PayMoney)
            return retExecResult(Constants.ExecResult.Error, "收款金额不能大于未收金额，或本开单已收款", "");
            
        entUpdBillInfo.PayDate = entBillInfo.PayDate;
        entUpdBillInfo.PayMoney = entBillInfo.PayMoney;
        entUpdBillInfo.PayOprEmpId = CurrentUserInfo.UserId;
        entUpdBillInfo.PayOprEmpName = CurrentUserInfo.UserName;
        entUpdBillInfo.PayOprEmpDepartId = CurrentUserInfo.DepartId;
        entUpdBillInfo.PayOprEmpDepart = CurrentUserInfo.DepartName;
        entUpdBillInfo.PayPlaceId = entBillInfo.PayPlaceId;
        entUpdBillInfo.PayPlace = entBillInfo.PayPlace;
        entUpdBillInfo.Status = 1;
        entUpdBillInfo.InvoiceCode = entBillInfo.InvoiceCode;
        entUpdBillInfo.InvoiceDate = entBillInfo.InvoiceDate;

        if (string.IsNullOrEmpty(entBillInfo.InvoiceCode))
        {
            entUpdBillInfo.InvoiceOprEmpId = CurrentUserInfo.UserId;
            entUpdBillInfo.InvoiceOprEmp = CurrentUserInfo.UserName;
            entUpdBillInfo.InvoiceOprDate = DateTime.Now;
        }
        if (entBillInfo.IsFinish == true)
        {
            entUpdBillInfo.IsFinish = true;
            entUpdBillInfo.AchievementYear = DateTime.Now.Year;
            entUpdBillInfo.AchievementMonth = DateTime.Now.Month;
            entUpdBillInfo.AchievementDate = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month;
            entUpdBillInfo.LastCreateArchDate = DateTime.Now;
            entUpdBillInfo.LastCreateArchEmp = CurrentUserInfo.UserName;
        }

        ContractFinanceBillInfo.Update(entUpdBillInfo);

        if (lsPayItems.Count > 0)
        {
            for (int idxCnt = 0; idxCnt < lsPayItems.Count; idxCnt++)
            {
                lsPayItems[idxCnt].BillId = entUpdBillInfo.BillId;
                lsPayItems[idxCnt].InputTime = DateTime.Now;
                lsPayItems[idxCnt].PayItemId = Guid.NewGuid();
                ContractFinancePayedItems.Add(lsPayItems[idxCnt]);
            }
        }

        //更新佣金数据
        addCommisionInfo(entUpdBillInfo);
        
        //
        string strMessage = "";
        if (entBillInfo.IsFinish) Chenxin.Lib.Business.BLL.ContractBaseInfo.CaculateFinanceDeductInfo(entUpdBillInfo.ContractId, out strMessage);

        string text = CurrentUserInfo.UserName + "：收付款登记。成交编号：" + entUpdBillInfo.ContractCode + "，单据编号：" + entUpdBillInfo.BillCode;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_AddPayment"].ToString());
        
        strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_RegPayment_Succ, "");

        return strRet;
    }

    //*******************************************
    //功能概要       ：更新佣金数据
    //参数説明       ：context 上下文 
    //返回值説明     ：--
    //功能詳細       ：更新佣金数据
    //作成者         ：he.jifeng
    //作成日         ：2010-09-06
    //*******************************************
    private void addCommisionInfo(Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo)
    {
        ContractCommisionInfo bllComm = new ContractCommisionInfo();

        Chenxin.Lib.Business.Entity.t_Contract_CommisionInfo entComm = ContractCommisionInfo.GetModel(entBillInfo.ContractId);

        if (entComm == null)
        {
            return;
        }
        
        if (entBillInfo.InOrOut.Equals(1))
        {
            switch (entBillInfo.FundName)
            {
                case "上家佣金":
                    entComm.VCommisionGeted = entComm.VCommisionGeted.Value + entBillInfo.PayMoney.Value;
                    break;
                case "下家佣金":
                    entComm.BCommisionGeted = entComm.BCommisionGeted.Value + entBillInfo.PayMoney.Value;
                    break;
                case "代收房款":
                    entComm.GetedSaleMoney = entComm.GetedSaleMoney.Value + entBillInfo.PayMoney.Value;
                    break;
                case "代收税费":
                    entComm.GetedTax = entComm.GetedTax.Value + entBillInfo.PayMoney.Value;
                    break;
                default:
                    break;
            }
        }
        else if (entBillInfo.InOrOut.Equals(-1))
        {
            switch (entBillInfo.FundName)
            {
                case "上家咨询费":
                    entComm.VConsultingPayed = entComm.VConsultingPayed.Value + entBillInfo.PayMoney.Value;
                    break;
                case "下家咨询费":
                    entComm.BConsultingPayed = entComm.BConsultingPayed.Value + entBillInfo.PayMoney.Value;
                    break;
                case "上家退佣":
                    entComm.VCommisionGeted = entComm.VCommisionGeted.Value - entBillInfo.PayMoney.Value;
                    break;
                case "下家退佣":
                    entComm.BCommisionGeted = entComm.BCommisionGeted.Value - entBillInfo.PayMoney.Value;
                    break;
                case "代付房款":
                    entComm.PayedSaleMoney = entComm.PayedSaleMoney.Value + entBillInfo.PayMoney.Value;
                    break;
                case "代付税费":
                    entComm.PayedTax = entComm.PayedTax.Value + entBillInfo.PayMoney.Value;
                    break;
                default:
                    break;
            }
        }

        ContractCommisionInfo.Update(entComm);
    }
    
    //*******************************************
    //功能概要       ：复核
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：复核
    //作成者         ：he.jifeng
    //作成日         ：2010-09-03
    //*******************************************
    private string checkBillInfo(HttpContext context)
    {
        string strRet = string.Empty;
        string BillId = context.Request.Params["modelid"];

        if (string.IsNullOrEmpty(BillId))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();
            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = ContractFinanceBillInfo.GetModel(new Guid(BillId));

            entBillInfo.IsRecheck = true;
            ContractFinanceBillInfo.Update(entBillInfo);

            string text = CurrentUserInfo.UserName + "：单据复核。成交编号：" + entBillInfo.ContractCode + "，单据编号：" + entBillInfo.BillCode;
            AddLog(text, BusinessType.BusinessTypeComboList()["OprType_CheckBill"].ToString());
            
            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_CheckBillInfo_Succ, "");
        }
        return strRet;
    }


    //*******************************************
    //功能概要       ：反复核
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：反复核
    //作成者         ：he.jifeng
    //作成日         ：2010-09-03
    //*******************************************
    private string unCheckBillInfo(HttpContext context)
    {
        string strRet = string.Empty;
        string BillId = context.Request.Params["modelid"];

        if (string.IsNullOrEmpty(BillId))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();
            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = ContractFinanceBillInfo.GetModel(new Guid(BillId));

            entBillInfo.IsRecheck = false;
            ContractFinanceBillInfo.Update(entBillInfo);

            string text = CurrentUserInfo.UserName + "：单据反复核。成交编号：" + entBillInfo.ContractCode + "，单据编号：" + entBillInfo.BillCode;
            AddLog(text, BusinessType.BusinessTypeComboList()["OprType_UnCheckBill"].ToString());
            
            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_UnCheckBillInfo_Succ, "");
        }
        return strRet;
    }

    //*******************************************
    //功能概要       ：作废收付款记录
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：作废收付款记录
    //作成者         ：he.jifeng
    //作成日         ：2010-09-05
    //*******************************************
    private string cancelPayment(HttpContext context)
    {
        string strRet = string.Empty;
        string BillId = context.Request.Params["BillId"];


        if (string.IsNullOrEmpty(BillId)) return retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = ContractFinanceBillInfo.GetModel(new Guid(BillId));

        //删除佣金
        clearCommisionInfo(entBillInfo);
        
        //
        entBillInfo.PayMoney = 0;
        entBillInfo.PayDate = null;
        entBillInfo.PayOprEmpDepart = "";
        entBillInfo.PayOprEmpDepartId = Guid.Empty;
        entBillInfo.PayOprEmpId = Guid.Empty;
        entBillInfo.PayOprEmpName = "";
        entBillInfo.PayPlace = "";
        entBillInfo.PayPlaceId = Guid.Empty;
        entBillInfo.IsRecheck = false;
        entBillInfo.InvoiceCode = "";
        entBillInfo.InvoiceDate = null;
        entBillInfo.InvoiceMemo = "";
        entBillInfo.InvoiceOprDate = null;
        entBillInfo.InvoiceOprEmp = "";
        entBillInfo.InvoiceOprEmpId = Guid.Empty;
        entBillInfo.AchievementDate = "";
        entBillInfo.AchievementMonth = null;
        entBillInfo.AchievementYear = null;
        entBillInfo.Status = 0;

        ContractFinanceBillInfo.Update(entBillInfo);

        //删除付款明细
        ContractFinancePayedItems.DeleteByBillId(entBillInfo.BillId);

        string text = CurrentUserInfo.UserName + "：作废收付款。成交编号：" + entBillInfo.ContractCode + "，单据编号：" + entBillInfo.BillCode;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_CancelPayment"].ToString());

        return retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_CancelPayment_Succ, "");
    }

    //*******************************************
    //功能概要       ：回补佣金数据
    //参数説明       ：context 上下文 
    //返回值説明     ：--
    //功能詳細       ：回补佣金数据
    //作成者         ：he.jifeng
    //作成日         ：2010-09-06
    //*******************************************
    private void clearCommisionInfo(Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo)
    {
        ContractCommisionInfo bllComm = new ContractCommisionInfo();

        Chenxin.Lib.Business.Entity.t_Contract_CommisionInfo entComm = ContractCommisionInfo.GetModel(entBillInfo.ContractId);

        if (entComm == null)
        {
            return;
        }

        if (entBillInfo.InOrOut.Equals(1))
        {
            switch (entBillInfo.FundName)
            {
                case "上家佣金":
                    entComm.VCommisionGeted = entComm.VCommisionGeted.Value - entBillInfo.PayMoney.Value;
                    break;
                case "下家佣金":
                    entComm.BCommisionGeted = entComm.BCommisionGeted.Value - entBillInfo.PayMoney.Value;
                    break;
                case "代收房款":
                    entComm.GetedSaleMoney = entComm.GetedSaleMoney.Value - entBillInfo.PayMoney.Value;
                    break;
                case "代收税费":
                    entComm.GetedTax = entComm.GetedTax.Value - entBillInfo.PayMoney.Value;
                    break;
                default:
                    break;
            }
        }
        else if (entBillInfo.InOrOut.Equals(-1))
        {
            switch (entBillInfo.FundName)
            {
                case "上家咨询费":
                    entComm.VConsultingPayed = entComm.VConsultingPayed.Value - entBillInfo.PayMoney.Value;
                    break;
                case "下家咨询费":
                    entComm.BConsultingPayed = entComm.BConsultingPayed.Value - entBillInfo.PayMoney.Value;
                    break;
                case "上家退佣":
                    entComm.VCommisionGeted = entComm.VCommisionGeted.Value + entBillInfo.PayMoney.Value;
                    break;
                case "下家退佣":
                    entComm.BCommisionGeted = entComm.BCommisionGeted.Value + entBillInfo.PayMoney.Value;
                    break;
                case "代付房款":
                    entComm.PayedSaleMoney = entComm.PayedSaleMoney.Value - entBillInfo.PayMoney.Value;
                    break;
                case "代付税费":
                    entComm.PayedTax = entComm.PayedTax.Value - entBillInfo.PayMoney.Value;
                    break;
                default:
                    break;
            }
        }

        ContractCommisionInfo.Update(entComm);
    }
    
    //*******************************************
    //功能概要       ：登记发票信息
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：登记发票信息
    //作成者         ：he.jifeng
    //作成日         ：2010-09-05
    //*******************************************
    private string regInvoice(HttpContext context)
    {
        string strRet = string.Empty;
        string ClientModel = context.Request.Params["model"];

        if (string.IsNullOrEmpty(ClientModel))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();
            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = (Chenxin.Lib.Business.Entity.ContractFinanceBillInfo)JsonHelp.Convert2Entity(ClientModel, typeof(Chenxin.Lib.Business.Entity.ContractFinanceBillInfo));

            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entUpdBillInfo = ContractFinanceBillInfo.GetModel(entBillInfo.BillId);

            entUpdBillInfo.InvoiceCode = entBillInfo.InvoiceCode;
            entUpdBillInfo.InvoiceDate = entBillInfo.InvoiceDate;
            entUpdBillInfo.InvoiceOprDate = DateTime.Now;
            entUpdBillInfo.InvoiceOprEmp = CurrentUserInfo.UserName;
            entUpdBillInfo.InvoiceOprEmpId = CurrentUserInfo.UserId;
            bool bCaculate = false;
            if (entBillInfo.IsFinish == true && entUpdBillInfo.IsFinish==false)
            {
                bCaculate = true;
                entUpdBillInfo.IsFinish = true;
                entUpdBillInfo.AchievementYear = DateTime.Now.Year;
                entUpdBillInfo.AchievementMonth = DateTime.Now.Month;
                entUpdBillInfo.AchievementDate = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month;
                entUpdBillInfo.LastCreateArchDate = DateTime.Now;
                entUpdBillInfo.LastCreateArchEmp = CurrentUserInfo.UserName;
            }

            ContractFinanceBillInfo.Update(entUpdBillInfo);

            string strMessage = "";
            if (bCaculate) Chenxin.Lib.Business.BLL.ContractBaseInfo.CaculateFinanceDeductInfo(entUpdBillInfo.ContractId, out strMessage);
            
            string text = CurrentUserInfo.UserName + "：登记发票。成交编号：" + entUpdBillInfo.ContractCode + "，发票编号：" + entUpdBillInfo.InvoiceCode;
            AddLog(text, BusinessType.BusinessTypeComboList()["OprType_RegInvoice"].ToString());
            
            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_RegInvoice_Succ, "");
        }
        
        return strRet;
    }
    
    //*******************************************
    //功能概要       ：发票领取记录
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：发票领取记录
    //作成者         ：he.jifeng
    //作成日         ：2010-09-05
    //*******************************************
    private string regInvoiceRev(HttpContext context)
    {
        string strRet = string.Empty;
        string ClientModel = context.Request.Params["model"];

        if (string.IsNullOrEmpty(ClientModel))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();
            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = (Chenxin.Lib.Business.Entity.ContractFinanceBillInfo)JsonHelp.Convert2Entity(ClientModel, typeof(Chenxin.Lib.Business.Entity.ContractFinanceBillInfo));

            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entUpdBillInfo = ContractFinanceBillInfo.GetModel(entBillInfo.BillId);

            entUpdBillInfo.InvoiceDate = DateTime.Now;
            entUpdBillInfo.InvoiceMemo = entBillInfo.InvoiceMemo;

            ContractFinanceBillInfo.Update(entUpdBillInfo);

            string text = CurrentUserInfo.UserName + "：发票领取。成交编号：" + entUpdBillInfo.ContractCode + "，发票编号：" + entUpdBillInfo.InvoiceCode;
            AddLog(text, BusinessType.BusinessTypeComboList()["OprType_RevInvoice"].ToString());
            
            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_RegInvoiceRev_Succ, "");
        }

        return strRet;
    }

    //*******************************************
    //功能概要       ：财务封帐
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：财务封帐
    //作成者         ：he.jifeng
    //作成日         ：2010-09-05
    //*******************************************
    private string closeAccount(HttpContext context)
    {
        string strRet = string.Empty;
        string ClientModel = context.Request.Params["model"];

        if (string.IsNullOrEmpty(ClientModel))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            int Year;
            int Month;
            ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();
            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entBillInfo = (Chenxin.Lib.Business.Entity.ContractFinanceBillInfo)JsonHelp.Convert2Entity(ClientModel, typeof(Chenxin.Lib.Business.Entity.ContractFinanceBillInfo));

            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entUpdBillInfo = ContractFinanceBillInfo.GetModel(entBillInfo.BillId);


            DateTime now = DateTime.Now;
            DateTime nowdt = new DateTime(now.Year, now.Month, 1);

            if (!CurrentUserInfo.ContainsPermit(Constants.Permit_Bill_ArchivementDate))
            {
                if (DateTime.Parse(entBillInfo.AchievementDate).CompareTo(nowdt) < 0)
                {
                    return "{success:'0',msg:'归属时间不能早于当前月，操作失败！',data:''}";
                }
            }

            //年
            Year = Convert.ToInt32(entBillInfo.AchievementDate.Substring(0, 4));
            //月
            Month = Convert.ToInt32(entBillInfo.AchievementDate.Substring(5, 2));


            entUpdBillInfo.IsFinish = true;
            entUpdBillInfo.AchievementYear = Convert.ToInt32(entBillInfo.AchievementDate.Substring(0, 4));
            entUpdBillInfo.AchievementMonth = Convert.ToInt32(entBillInfo.AchievementDate.Substring(5, 2));
            entUpdBillInfo.AchievementDate = entUpdBillInfo.AchievementYear.ToString() + "." + entUpdBillInfo.AchievementMonth.ToString();
            entUpdBillInfo.LastCreateArchDate = DateTime.Now;
            entUpdBillInfo.LastCreateArchEmp = CurrentUserInfo.UserName;

            ContractFinanceBillInfo.Update(entUpdBillInfo);

            string strMessage = "";
            Chenxin.Lib.Business.BLL.ContractBaseInfo.CaculateFinanceDeductInfo(entUpdBillInfo.ContractId, out strMessage);
            
            string text = CurrentUserInfo.UserName + "：财务封帐。成交编号：" + entUpdBillInfo.ContractCode + "，单据编号：" + entUpdBillInfo.BillCode;
            AddLog(text, BusinessType.BusinessTypeComboList()["OprType_CloseAccount"].ToString());

            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_CloseAccount_Succ, "");

        }
        
        return strRet;
    }

    //*******************************************
    //功能概要       ：财务解帐
    //参数説明       ：context 上下文 
    //返回值説明     ：回传客户端Json数据
    //功能詳細       ：财务解帐
    //作成者         ：he.jifeng
    //作成日         ：2010-09-05
    //*******************************************
    private string openAccount(HttpContext context)
    {
        string strRet = string.Empty;
        string BillId = context.Request.Params["BillId"];
        if (string.IsNullOrEmpty(BillId))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, ""); 
        }
        else
        {
            ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();
            Chenxin.Lib.Business.Entity.ContractFinanceBillInfo entUpdBillInfo = ContractFinanceBillInfo.GetModel(new Guid(BillId));

            entUpdBillInfo.IsFinish = false;
            entUpdBillInfo.AchievementDate = "";
            entUpdBillInfo.AchievementMonth = null;
            entUpdBillInfo.AchievementYear = null;
            entUpdBillInfo.LastCreateArchDate = null;
            entUpdBillInfo.LastCreateArchEmp = "";

            ContractFinanceBillInfo.Update(entUpdBillInfo);

            string strMessage = "";
            Chenxin.Lib.Business.BLL.ContractBaseInfo.CaculateFinanceDeductInfo(entUpdBillInfo.ContractId, out strMessage);

            string text = CurrentUserInfo.UserName + "：财务解帐。成交编号：" + entUpdBillInfo.ContractCode + "，单据编号：" + entUpdBillInfo.BillCode;
            AddLog(text, BusinessType.BusinessTypeComboList()["OprType_OpenAccount"].ToString());
            
            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_OpenAccount_Succ, "");
        }
        return strRet;
    }
    //*******************************************
    //功能概要       ：自定义权限检查
    //参数説明       ：strMsg 消息 
    //返回值説明     ：有无权限
    //功能詳細       ：自定义权限检查
    //作成者         ：he.jifeng
    //作成日         ：2010-09-03
    //*******************************************
    public override bool CustomerCheckPermint(HttpContext context, out string strMsg)
    {
        string op = context.Request["op"].ToLower();
        if (op != null)
        {
            if (op.Equals("checkpermit"))
            {
                string Code = context.Request.Params["code"];
                switch (Code)
                {
                    //开单作废
                    case "cancelbill":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_CancelBillInfo))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //收付款登记
                    case "regpayment":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_RegPayment))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //作废收付款
                    case "cancelpayment":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_CancelPayment))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //登记发票
                    case "reginvoice":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_RegInvoice))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //发票领取记录
                    case "reginvoicerev":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_RegInvoiceRev))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //复核
                    case "check":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_CheckBillInfo))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //反复核
                    case "uncheck":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_UnCheckBillInfo))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //财务封帐
                    case "closeaccout":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_CloseAccout))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        else
                        {
                            if (CurrentUserInfo.ContainsPermit(Constants.Permit_Bill_ArchivementDate))
                            {
                                strMsg = "{success:1,msg:'',data:{permit:true,selectAllDate:true}}";
                                return true;
                            }
                            else
                            {
                                strMsg = "{success:1,msg:'',data:{permit:true,selectAllDate:false}}";
                                return true;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        strMsg = "{success:1,msg:'',data:{permit:true}}";
        return true;
    }
}